/**
 * Created by JasonChou on 16/5/9.
 */
import Promise from './promise'
import {wx as rowWx} from './wxsdk'
import http from './http'

var wxready = false;

if (agent.isWechat) {
    !function () {
        var wxUrl = encodeURIComponent(location.href.split('#')[0]);
        http.get({
            url: api + 'wx/jsSDKTicket',
            data: {
                url: wxUrl
            },
            jsonp: true
        }).then(function (obj) {
            var appId = obj.data.app_id,
                timestamp = obj.data.timestamp,
                nonceStr = obj.data.noncestr,
                signature = obj.data.signature;
            rowWx.config({
                appId: appId,
                timestamp: timestamp,
                nonceStr: nonceStr,
                signature: signature,
                jsApiList: [
                    'onMenuShareTimeline',
                    'onMenuShareAppMessage',
                    'hideAllNonBaseMenuItem',
                    'showAllNonBaseMenuItem',
                    'showOptionMenu',
                    'showMenuItems',
                    'hideMenuItems',
                ]
            });
            rowWx.ready(function () {
                mod.check.resolve();
            })
        })
    }();
}

rowWx.ready(function () {
    wxready = true;
    if (typeof cb == 'function')cb();
});

var mod = {
    ready: function (callback) {
        if (wxready) {
            callback();
        } else {
            cb = callback;
        }
    },

    init:function(){
        rowWx.hideAllNonBaseMenuItem();
        rowWx.showMenuItems({
            menuList: [
                'menuItem:share:appMessage',
                'menuItem:share:timeline'
            ] // 要显示的菜单项，所有menu项见附录3
        });

        var shareData = {
            title: '纹身大咖正品器材商城上线啦~',
            desc: '开业大优惠，限时发放800元红包，新老用户享不停',
            link: 'http://qicai.wenshendaka.com',
            imgUrl: 'http://qicai.wenshendaka.com/static/img/share_icon.jpg'
        };

        rowWx.onMenuShareAppMessage(shareData);
        rowWx.onMenuShareTimeline(shareData);
    },

    shareAllow: function () {

    },
    closeAll: function () {
        rowWx.hideOptionMenu();
        rowWx.hideAllNonBaseMenuItem();
    },
    setShareParams: function (options) {
        //todo 分享参数的设置;
    },
    check: new Promise()
};

mod.check.done(function () {
    mod.init();
});

export {mod as wx}